--
-- Created by IntelliJ IDEA.
-- User: bluda
-- Date: 2018/8/21
-- Time: 11:12
-- To change this template use File | Settings | File Templates.
-- 战场中的军队


local FightUnit = import("app.model.battlefield.FightUnit")

local Military = {
    id = nil,
    battlefield = nil,
    name = "无敌军团",
    keyUnits = Set(),
    units = Set(),
    squads = Set(),
    formation = "ziyou",
    morale = 100,
    mana = 100,
    food = 100,
    money = 100,
    unitCounter = 0,
    target = nil,
    boss = nil,


    moraleUI = nil,
    manaUI = nil,
    foodUI = nil,
    money = nil
}

function Military:setTargetMilitary(target)
    self.target = target
end

function Military:getTargetMilitary()
    return self.target
end

-- 得到所有战斗单位
function Military:getUnits()
    return self.units
end

-- 得到关键战斗单元
function Military:getKeyUnits()
    return self.keyUnits
end

function Military:process(dt, ct, ci)
--    for k,unit in pairs(self.units) do
--        unit:process(dt, ct, ci)
--    end
    for k,squad in pairs(self.squads) do
        squad:process(dt, ct, ci)
    end
end

function Military:initUI(node, left)
    if left == true then
        -- 士气值，总生命值，撤退按钮
        local moraleUI = cc.Label:createWithSystemFont("士气:" .. self.morale, "宋体", 12)
        local sceneArea = self.battlefield.sceneArea
        moraleUI:move(sceneArea.x + 30, sceneArea.height - 10):addTo(node)
        self.moraleUI = moraleUI
    
        local manaUI = cc.Label:createWithSystemFont("技能值:" .. self.mana, "宋体", 12)
        local sceneArea = self.battlefield.sceneArea
        manaUI:move(sceneArea.x + 100, sceneArea.height - 10):addTo(node)
        self.manaUI = manaUI
    
        local foodUI = cc.Label:createWithSystemFont("粮食:" .. self.food, "宋体", 12)
        local sceneArea = self.battlefield.sceneArea
        foodUI:move(sceneArea.x + 180, sceneArea.height - 10):addTo(node)
        self.foodUI = foodUI
    else
        -- 士气值，总生命值，撤退按钮
        local moraleUI = cc.Label:createWithSystemFont("士气:" .. self.morale, "宋体", 12)
        local sceneArea = self.battlefield.sceneArea
        moraleUI:move(sceneArea.width - 30, sceneArea.height - 10):addTo(node)
        self.moraleUI = moraleUI

        local manaUI = cc.Label:createWithSystemFont("技能值:" .. self.mana, "宋体", 12)
        local sceneArea = self.battlefield.sceneArea
        manaUI:move(sceneArea.width - 100, sceneArea.height - 10):addTo(node)
        self.manaUI = manaUI

        local foodUI = cc.Label:createWithSystemFont("粮食:" .. self.food, "宋体", 12)
        local sceneArea = self.battlefield.sceneArea
        foodUI:move(sceneArea.width - 180, sceneArea.height - 10):addTo(node)
        self.foodUI = foodUI
    end
end

function Military:makeSquad(params, position)
    local units = {}
    for i = 1, 100 do
        local unit = self:makeFightUnit(params, position)
        units[i] = unit
    end
    local squad = Squad:create(units[1], units)
    self.squads:insert(squad)
end

function Military:makeFightUnit(params, position)
    local class = params.class
    
    local solder = class:create(self.battlefield, self, self.name .. "_步兵_" .. math.random(1000,10000))
    self.unitCounter = self.unitCounter + 1
    for k,v in pairs(params) do
        --print(k, v())
        if type(v) == "function" then
            solder[k] = v()
        elseif type(v) == "string" or type(v) == "number" or type(v) == "boolean" then
            solder[k] = v
        else 
        end
    end
    --xprint(solder, 1)
    solder:setPosition(position.x, position.y)
    logD(self.battlefield:getGridValue(position))

    solder:initUI(solder.resPrefix, solder.resStand, solder.resMove, solder.resAttack, solder.resStand, solder.resDeath)
    solder:addToUI(self.battlefield:getSceneNode())
    solder:stand()
    if self.gong then
        solder.ui:setColor(cc.c3b(222,222,222))
    else
        solder.ui:setColor(cc.c3b(66,88,22))
    end
    
    self.units:insert(solder)
    self.keyUnits:insert(solder)
    
    return solder
end

function Military:create(battlefield, name, userdata, gong)
    local mly = xcopy(Military)
    mly.battlefield = battlefield
    mly.name = name
    mly.gong = gong
    mly.id = userdata.id

    return mly
end

function Military:getAllUnitCount()
    return self.unitCounter
end

function Military:cleanUnit(unit)
    self.units:remove(unit)
    self.keyUnits:remove(unit)
    self.battlefield:cleanUnit(unit)
end

function Military:getId()
    return self.id
end



return Military